package com.zhong.gang.applib.http.callback;

import android.content.Context;
import android.text.TextUtils;

import com.zhong.gang.applib.http.LoadingDialog;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public abstract class Callback<T> {

    private LoadingDialog dialog;


    /**
     * UI Thread
     *
     * @param request
     */
    public void onBefore(Context context,String title,Request request, int id) {
        if (context == null || TextUtils.isEmpty(title))
            dialog = null;
        else if (dialog == null)
            dialog = new LoadingDialog(context);
        if (dialog != null)
            dialog.show(title);
    }

    /**
     * UI Thread
     *
     * @param request
     */
    public void onBefore(Request request, int id) {}

    /**
     * UI Thread
     *
     * @param
     */
    public void onAfter(int id) {
        if (dialog != null)
            dialog.dismiss();
    }

    /**
     * UI Thread
     *
     * @param progress
     */
    public void inProgress(float progress, long total , int id) {

    }

    /**
     * if you parse reponse code in parseNetworkResponse, you should make this method return true.
     *
     * @param response
     * @return
     */
    public boolean validateReponse(Response response, int id)
    {
        return response.isSuccessful();
    }

    /**
     * Thread Pool Thread
     *
     * @param response
     */
    public abstract T parseNetworkResponse(Response response, int id) throws Exception;

    public void onError(Call call, Exception e, int id){
//        ToastUtils.show(e.toString());
    }

    public abstract void onResponse(T response, int id);


    public static Callback CALLBACK_DEFAULT = new Callback() {

        @Override
        public Object parseNetworkResponse(Response response, int id) throws Exception {
            return null;
        }

        @Override
        public void onError(Call call, Exception e, int id) {

        }

        @Override
        public void onResponse(Object response, int id) {

        }
    };

}